<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Forum_model extends CI_Model {
	
	function __construct(){
        parent::__construct();
    }
    
    
	function arrays($key){
    	$data = array(
    		'forum_status' 			=> array("0"=>"待审核","1"=>"审核通过","2"=>"审核失败"),
    		'forum_shield' 			=> array("0"=>"否","1"=>"是"),
    	);
    	
    	if($key && isset($data[$key])){
    		return $data[$key];
    	}else{
    		return array();
    	}
    }
    
    /**
     * 
     * 添加版块
     * @param $data
     */
	function add_section($data = array()){
		$this->db->insert('rsh_forum_section', $data);
		return $this->db->insert_id();
	}
	
	
	/**
	 * 
	 * 编辑版块
	 * @param $id
	 * @param $data
	 */
	function update_section($id, $data = array()){
		$this->mycache->delete("section_obj_".$id);
		$this->mycache->delete("sname_list");
		$this->db->where('id', $id);
		$this->db->update('rsh_forum_section', $data);
		return $this->db->affected_rows();
	}
	
	/**
	 * 
	 * 查询单个版块
	 * @param $id
	 */
	function get_section_obj($id){
		$mkey = "section_obj_".$id;
		$ret = $this->mycache->get_array($mkey);
		
		if($ret != false){
			return $ret;
		}
		$select = "id,name,is_del";
		$this->db->select($select);
		$this->db->where(array('id'=>$id));
		$query = $this->db->get('rsh_forum_section');
		$retsult = $query->row_array();
		$this->mycache->set_array($mkey,$retsult);
		return $retsult;
	}
    
	/**
	 * 
	 * 版块列表
	 * @param $data
	 */	
	function section_list($data){
		//封装查询内容
		$select = "id,name,is_del";
		
		//封装条件
		$_where = " where is_del=0 ";
		
		//排序
		$order  = " order by id desc ";
		//封装sql
		$page_size = $data['page_size'];
		$page_start = $data['page_start'];
		$sql_result = "select $select from rsh_forum_section";
		$sql_result .= $_where;
		$sql_result .= $order;
		$sql_result .= " limit $page_start, $page_size";
		$query = $this->db->query($sql_result);
		$data['forum_list'] = $query->result_array();
		
		//总记录条数
		$sql_count = "select count(*) as count from rsh_forum_section";
		$sql_count .= $_where;
		$query = $this->db->query($sql_count);
		$data['forum_count'] = $query->row()->count;
		return $data;
	}
	
	
	
	
	/**
     * 
     * 添加主题
     * @param $data
     */
	function add_theme($data = array()){
		$this->db->insert('rsh_forum_theme', $data);
		return $this->db->insert_id();
	}
	
	
	/**
	 * 
	 * 编辑主题
	 * @param $id
	 * @param $data
	 */
	function update_theme($id, $data = array()){
		$this->mycache->delete("theme_obj_".$id);
		$this->mycache->delete("tname_list");
		
		$this->db->where('id', $id);
		$this->db->update('rsh_forum_theme', $data);
		return $this->db->affected_rows();
	}
	
	/**
	 * 
	 * 查询单个主题
	 * @param $id
	 */
	function get_theme_obj($id){
		$mkey = "theme_obj_".$id;
		$ret = $this->mycache->get_array($mkey);
		
		if($ret != false){
			return $ret;
		}
		$select = "id,name,is_del,description,thumbnail";
		$this->db->select($select);
		$this->db->where(array('id'=>$id));
		$query = $this->db->get('rsh_forum_theme');
		$retsult = $query->row_array();
		$this->mycache->set_array($mkey,$retsult);
		return $retsult;
	}
    
	/**
	 * 
	 * 主题列表
	 * @param $data
	 */	
	function theme_list($data){
		//封装查询内容
		$select = "id,name,is_del,description,thumbnail";
		
		//封装条件
		$_where = " where is_del=0 ";
		
		//排序
		$order  = " order by id desc ";
		//封装sql
		$page_size = $data['page_size'];
		$page_start = $data['page_start'];
		$sql_result = "select $select from rsh_forum_theme";
		$sql_result .= $_where;
		$sql_result .= $order;
		$sql_result .= " limit $page_start, $page_size";
		$query = $this->db->query($sql_result);
		$data['forum_list'] = $query->result_array();
		
		//总记录条数
		$sql_count = "select count(*) as count from rsh_forum_theme";
		$sql_count .= $_where;
		$query = $this->db->query($sql_count);
		$data['forum_count'] = $query->row()->count;
		return $data;
	}
	
	
	
	
	 /**
     * 
     * 添加帖子
     * @param $data
     */
	function add_forum_post($data = array()){
		$this->db->insert('rsh_forum_post', $data);
		return $this->db->insert_id();
	}
	
	
	/**
	 * 
	 * 编辑帖子
	 * @param $id
	 * @param $data
	 */
	function update_forum_post($id, $data = array()){
		$this->mycache->delete("forum_post_obj_".$id);
		$this->db->where('id', $id);
		$this->db->update('rsh_forum_post', $data);
		return $this->db->affected_rows();
	}
	
	
	/**
	 * 回帖次数+1
	 * @param $id 帖子ID
	 */
	function set_recovery_post($id){
		$sql = "update rsh_forum_post set recovery=recovery+1 where id='{$id}'";
		$query = $this->db->query($sql);
		return $this->db->affected_rows(); 
	}
		/**
	 * 点击次数+1
	 * @param $id 帖子ID
	 */
	function set_hits_post($id){
		$sql = "update rsh_forum_post set hits=hits+1 where id='{$id}'";
		$query = $this->db->query($sql);
		return $this->db->affected_rows(); 
	}
	/**
	 * 
	 * 查询单个帖子
	 * @param $id
	 */
	function get_forum_post_obj($id){
		$mkey = "forum_post_obj_".$id;
		/*$ret = $this->mycache->get_array($mkey);
		
		if($ret != false){
			return $ret;
		}*/
		$select = "id,fid,tid,author,authorid,subject,content,add_time,fstatus,tags,recovery,folder,is_shield,first,hits";
		$this->db->select($select);
		$this->db->where(array('id'=>$id));
		$query = $this->db->get('rsh_forum_post');
		$retsult = $query->row_array();
		//$this->mycache->set_array($mkey,$retsult);
		return $retsult;
	}
	
	
	/**
	 * 
	 * 主题列表
	 * @param $data
	 */	
	function invitation_list($data){
		//封装查询内容
		$select = "id,fid,tid,author,authorid,subject,content,add_time,fstatus,tags,folder,recovery,is_shield,first";
		
		//封装条件
		$_where = " where 1=1 ";
		
		if(isset($data['where']['fid']) && $data['where']['fid'] >0){
			$_where .= " and fid=".$data['where']['fid'];
		}
		
		if(isset($data['where']['tid']) && $data['where']['tid'] >0){
			$_where .= " and tid=".$data['where']['tid'];
		}
		
		if(isset($data['where']['authorid']) && $data['where']['authorid'] >0){
			$_where .= " and authorid=".$data['where']['authorid'];
		}
		
		
		if(isset($data['where']['subject']) && $data['where']['subject'] != ''){
			$_where .= " and subject like '%".$data['where']['subject']."%'";
		}
		
		if(isset($data['fstatus']) && $data['fstatus'] >0){
			$_where .= " and fstatus=".$data['fstatus'];
		}
		
		if(isset($data['first'])  && $data['first'] >0){
			$_where .= " and first=".$data['first'];
		}
		
		if(isset($data['folder'])  && $data['folder'] >0){
			$_where .= " and folder=".$data['folder'];
		}
		
		if(isset($data['is_shield'])){
			$_where .= " and is_shield=".$data['is_shield'];  
		}
		
		if(isset($data['where']['folder'])  && $data['where']['folder'] >0){
			$_where .= " and folder=".$data['where']['folder'];
		}
		
		
		
		//排序
		$order  = " order by add_time desc,id desc ";
		//封装sql
		$page_size = $data['page_size'];
		$page_start = $data['page_start'];
		$sql_result = "select $select from rsh_forum_post";
		$sql_result .= $_where;
		$sql_result .= $order;
		if($page_size > 0){
			$sql_result .= " limit $page_start, $page_size";
		}
		
		$query = $this->db->query($sql_result);
		$data['forum_list'] = $query->result_array();
		
		//总记录条数
		$sql_count = "select count(*) as count from rsh_forum_post";
		$sql_count .= $_where;
		$query = $this->db->query($sql_count);
		$data['forum_count'] = $query->row()->count;
		return $data;
	}
	
	
	
	/**
     * 
     * 添加帖子评论
     * @param $data
     */
	function add_comment($data = array()){
		$this->db->insert('rsh_forum_comment', $data);
		return $this->db->insert_id();
	}
	
	/**
     * 
     * 编辑帖子评论
     * @param $data
     */
	function edit_comment($id,$data){
		$this->db->where('id', $id);
		$this->db->update('rsh_forum_comment', $data);
		return $this->db->affected_rows();
	}
	
		
	/**
	 * 
	 * 帖子评论列表
	 * @param $data
	 */	
	function comment_list($data){
		//封装查询内容
		$select = "id,pid,author,authorid,comment,add_time,is_del";
		
		//封装条件
		$_where = " where 1=1 and is_del = 0 ";
		
		if(isset($data['where']['pid']) && $data['where']['pid'] >0){
			$_where .= " and pid=".$data['where']['pid'];
		}
		
		if(isset($data['where']['is_floor']) && $data['where']['is_floor'] >0){
			$_where .= " and is_floor=".$data['where']['is_floor'];
		}
		
		
		//排序
		$order  = " order by id asc ";
		//封装sql
		$page_size = $data['page_size'];
		$page_start = $data['page_start'];
		$sql_result = "select $select from rsh_forum_comment";
		$sql_result .= $_where;
		$sql_result .= $order;
		if($page_size > 0){
			$sql_result .= " limit $page_start, $page_size";
		}
		
		$query = $this->db->query($sql_result);
		$data['forum_list'] = $query->result_array();
		
		//总记录条数
		$sql_count = "select count(*) as count from rsh_forum_comment";
		$sql_count .= $_where;
		$query = $this->db->query($sql_count);
		$data['forum_count'] = $query->row()->count;
		return $data;
	}
	
	
	function get_tname_list(){
		$key = 'tname_list';
		$list = $this->mycache->get_array($key);
		if(!$list){
			$this->db->select('id,name');
			$this->db->where(array('is_del'=>0));		
			$query = $this->db->get('rsh_forum_theme');		
			$result = $query->result_array();
			$list = array();
			if($result){
				foreach($result as $v){
					$list[$v['id']] = $v['name'];
				}
				$this->mycache->set_array($key,$list);
			}
		}
		return $list;
	}
	
	function get_sname_list(){
		$key = 'sname_list';
		$list = $this->mycache->get_array($key);
		if(!$list){
			$this->db->select('id,name');
			$this->db->where(array('is_del'=>0));		
			$query = $this->db->get('rsh_forum_section');		
			$result = $query->result_array();
			$list = array();
			if($result){
				foreach($result as $v){
					$list[$v['id']] = $v['name'];
				}
				$this->mycache->set_array($key,$list);
			}
		}
		return $list;
	}
	
	
	/**
	 * 
	 * 论坛前台信息
	 */
	function forum_index(){
		
		$sql_result = "select id,name,description,thumbnail from rsh_forum_theme where is_del=0 order by id ASC ";
		
		$query = $this->db->query($sql_result);
		
		$forum_list = $query->result_array();
		
		$list = array();
		foreach ($forum_list as $val){
			$sql = "select id,subject from rsh_forum_post where fstatus=1 and is_shield=0 and tid=".$val['id']."  order by id desc  limit 4 ";
			$tquery = $this->db->query($sql);
			$theme_list = $tquery->result_array();
			$val['theme_list'] = $theme_list;
			$list[] = $val;
		}
		
		return $list;
	}

	/**
	 * 
	 * 统计帖子
	 */
	function count(){
		//总记录条数
		$sql_count = "select count(1) as count from rsh_forum_post where FROM_UNIXTIME(add_time,'%Y-%m-%d')='".date("Y-m-d")."'";
		$query = $this->db->query($sql_count);
		$data['today_count'] = $query->row()->count;
		$sql_count = "select count(1) as count from rsh_forum_post where FROM_UNIXTIME(add_time,'%Y-%m-%d')='".date('Y-m-d',strtotime('-1 day'))."'";
		$query = $this->db->query($sql_count);
		$data['yesterday_count'] = $query->row()->count;
		$sql_count = "select count(1) as count from rsh_forum_post";
		$query = $this->db->query($sql_count);
		$data['count'] = $query->row()->count;
		$sql_count = "select count(1) as count from rsh_user";
		$query = $this->db->query($sql_count);
		$data['user_count'] = $query->row()->count;
		return $data;
	}
	
	
}